package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Member;
import com.ruoyi.system.service.ICustomerService;
import com.ruoyi.system.service.IMemberService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Share;
import com.ruoyi.system.service.IShareService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户分享Controller
 * 
 * @author ruoyi
 * @date 2020-08-03
 */
@Controller
@RequestMapping("/system/share")
public class ShareController extends BaseController
{
    private String prefix = "system/share";

    @Autowired
    private IShareService shareService;
    @Autowired
    private IMemberService memberService;
    @Autowired
    private ICustomerService customerService;

    @RequiresPermissions("system:share:view")
    @GetMapping()
    public String share()
    {
        return prefix + "/share";
    }

    /**
     * 查询客户分享列表
     */
    @RequiresPermissions("system:share:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Share share)
    {
        startPage();
        List<Share> list = shareService.selectShareList(share);
        for(Share se:list){
            Member member = memberService.selectMemberById(se.getMemberId());
            se.setNickName(member.getNickName());
            se.setRealName(member.getRealName());
            se.setTelephone(member.getTelephone());
        }
        return getDataTable(list);
    }


    /**
     * 选择业务员树
     */
    @GetMapping(value = { "/selectMemberTree/{id}", "/selectMemberTree/" })
    public String selectMemberTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("customer", customerService.selectCustomerById(id));
        }
        return "system/customer/member_tree";
    }

    /**
     * 加载业务员树列表
     */
    @GetMapping("/memberTreeData")
    @ResponseBody
    public List<Ztree> memberTreeData(Long customerId)
    {
        List<Ztree> ztrees = memberService.selectMemberTreeShare(customerId);
        return ztrees;
    }

    /**
     * 新增保存客户分享
     */
    @Log(title = "客户分享", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Share share)
    {
        return toAjax(shareService.insertShare(share));
    }

    /**
     * 删除客户分享
     */
    @RequiresPermissions("system:share:remove")
    @Log(title = "客户分享", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(shareService.deleteShareByIds(ids));
    }



}
